<?php

namespace mpend\modules\commission\controllers;

use Yii;
use mpend\components\Controller;
use common\models\app\AppMenu;
use common\models\app\AppMenuPosition;
use common\models\attachs\AttachsVideo;
use yii\data\ActiveDataProvider;
use common\models\shop\ShopGoods;

class VideoController extends Controller {

    public function actionIndex() {
        $directoryAsset = Yii::$app->assetManager->getPublishedUrl('@webroot/themes');
        //菜单
        $shopMenuPosition = AppMenuPosition::findOne(1);
        $menus = $shopMenuPosition->getRenderMenus($this->module->level)->all();

        $query = AppMenu::find()->with('videosRecommend')->where([
            'and',
            ['position_id' => $shopMenuPosition->id],
            ['>=', 'level', $this->module->level],
        ])->orderBy(['listorder' => SORT_ASC]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => false,
        ]);

        //$headerOptions
        $headerOptions = [
            'class' => 'video-promotion'
        ];
        $_background = parent::getSiteConfigs('video-top-background');
        if ($_background !== null) {
            $headerOptions['style'] = 'background-image:url("' . $_background . '");';
        }

        return $this->render('index', [
            'headerOptions' => $headerOptions,
            'directoryAsset' => $directoryAsset,
            'menus' => $menus,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionList($id) {
        $model = AppMenu::findOne($id);
        $dataProvider = new ActiveDataProvider([
            'query' => AttachsVideo::find()->where(['category_id' => $id]),
            'pagination' => false,
        ]);
        //$headerOptions
        $headerOptions = [
            'class' => 'video-promotion video-promotion-blue'
        ];
        $_background = parent::getSiteConfigs('video-cate-background');
        if ($_background !== null) {
            $headerOptions['style'] = 'background-image:url("' . $_background . '");';
        }

        return $this->render('list', [
            'headerOptions' => $headerOptions,
            'model' => $model,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionView($id) {
        $model = AttachsVideo::findOne($id);
        $video = $model->video;
        //获取退广产品
        $recommend = ShopGoods::spread($this->uid);
        //获取用户上级的会员推广二维码
        $qrcodeUrl = $this->user->upperQrcode;
        return $this->render('view', [
            'video' => $video,
            'model' => $model,
            'recommend' => $recommend,
            'qrcodeUrl' => $qrcodeUrl,
        ]);
    }

    /**
     * actionFunctionName
     * Description
     *
     * @view template.php
     * @return mixed
     */
    public function actionFunctionName() {
        $dataProvider = new ActiveDataProvider([
            'query' => ModelClassName::find(),
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('template', [
            'dataProvider' => $dataProvider
        ]);
    }

}
